package com.lkd.http.controller;

import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.ConvertUtils;
import com.lkd.vo.OrderVO;
import com.lkd.vo.Pager;
import com.lkd.vo.PayVO;
import com.lkd.wxpay.WXConfig;
import com.lkd.wxpay.WxPayDTO;
import com.lkd.wxpay.WxPaySDKUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WxPaySDKUtil wxPaySDKUtil;


    /**
     * 订单查询
     *
     * @param pageIndex
     * @param pageSize
     * @param orderNo
     * @param openId
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/search")
    public Pager<OrderVO> search(
            @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "orderNo", required = false, defaultValue = "") String orderNo,
            @RequestParam(value = "openId", required = false, defaultValue = "") String openId,
            @RequestParam(value = "startDate", required = false, defaultValue = "") String startDate,
            @RequestParam(value = "endDate", required = false, defaultValue = "") String endDate) {
        return orderService.search(pageIndex, pageSize, orderNo, openId, startDate, endDate);
    }


    /**
     * 获取商圈下3个月内销量前10商品
     *
     * @param businessId
     * @return
     */
    @GetMapping("/businessTop10/{businessId}")
    public List<Long> getBusinessTop10Skus(@PathVariable Integer businessId) {
        return orderService.getTop10Sku(businessId);
    }

    @PostMapping("/requestPay")
    public String sendPayRequest(@RequestBody PayVO payVO) {
        OrderEntity order = orderService.createOrder(payVO);
        //将发送支付请求需要的信息封装到WxPayDTO中
        WxPayDTO wxPayDTO = new WxPayDTO();
        wxPayDTO.setBody(order.getSkuName());
        wxPayDTO.setOpenid(order.getOpenId());
        wxPayDTO.setOutTradeNo(order.getOrderNo());
        wxPayDTO.setTotalFee(order.getAmount());
        wxPayDTO.setExpireSecond(900);

        String result = wxPaySDKUtil.requestPay(wxPayDTO);
        return result;
    }

    /**
     * 微信支付回调接口
     *
     * @param request
     * @return
     */
    @RequestMapping("/payNotify")
    @ResponseBody
    public void payNotify(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            ServletInputStream inputStream = request.getInputStream();
//            String inputMsg = ConvertUtils.convertToString(inputStream);
//            String result = wxPaySDKUtil.validPay(inputMsg);
//            if (null != result) {
//                response.getWriter().write(WXConfig.RESULT);
//                //调用出货方法
//                orderService.giveProduct();
//            }
//        } catch (Exception e) {
//            log.error("支付回调失败");
//        }
//

        orderService.giveProduct("010000016620657095909135");
    }
}
